﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("article")]
    public class ArticleController : AdminController {

        private IArticleService _iArticleService;
        public ArticleController(IArticleService iArticleService) {
            _iArticleService = iArticleService;
        }

        [HttpGet]
        public JsonResult Gets(ArticleQuery query) {
            query.status = ArticleStatus.All;
            var model = _iArticleService.GetArticles(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult Get(long id) {
            var model = _iArticleService.GetArticle(id);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetDetails(ReplyArticleQuery query) {
            var model = _iArticleService.GetReplyArticles(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [HttpDelete]
        public JsonResult DeleteItem(long id) {
            var model = _iArticleService.DeleteReplyArticle(id , currentUser.user_id);
            return Json(ResultToJson.Json(model > 0? "删除成功" : "删除失败", ""), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult Reply(reply_article_dto dto) {
            dto.merchant_id = currentUser.user_id;
            var model = _iArticleService.ReplyArticle(dto);
            return Json(ResultToJson.Json(model > 0 ? "回复成功" : "回复失败", ""), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Save(article_dto dto) {
            dto.merchant_id = currentUser.user_id;
            var model = _iArticleService.AddArticle(dto);
            return Json(ResultToJson.Json(model> 0 ? "保存成功" : "保存失败", ""), JsonRequestBehavior.AllowGet);
        }

        [HttpDelete]
        public JsonResult Delete(long id) {
            article_status dto = new article_status { id = id, status = ArticleStatus.Delete, is_top = false, is_highlight = false };
            var model = _iArticleService.UpdateArticle(dto);
            return Json(ResultToJson.Json(model > 0 ? "删除成功" : "删除失败", ""), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult Quality(long id) {
            article_status dto = new article_status { id = id, is_top = false, is_highlight = true };
            var model = _iArticleService.UpdateArticle(dto);
            return Json(ResultToJson.Json(model > 0 ? "加入精品栏成功" : "加入精品栏失败", ""), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult Top(long id) {
            article_status dto = new article_status { id = id, is_top = true, is_highlight = false };
            var model = _iArticleService.UpdateArticle(dto);
            return Json(ResultToJson.Json(model > 0 ? "置顶成功" : "置顶失败", ""), JsonRequestBehavior.AllowGet);
        }
    }
}